<?php

	class EbController
	{
		static public $request;
		static public $response;
		static public $session;
		static public $filterManager;
		static public $commandEventMap;
		
		static private $time_start;
		
		static public function init()
		{
			self::$time_start = microtime(true);
			
			self::$request = new EbRequest();
			self::$response = new EbResponse();
			self::$session = new EbSession();

			self::$commandEventMap = EbApplication::$commandEventMap;
			self::$session->init();
			
			foreach (EbApplication::$listFilters AS $filter)
			{		
				$filter->init($filter->data);
			}
		}
		
		/**
		* This function is called firstly, we go execute all the prefilter, execute the request command then execute all the postfilter end render the response
		*/
		static public function processRequest()
		{			
			self::doFilter(self::$request,null);
			
			self::dispatchEvent(self::$request->event);
			
			if (!is_null(EbApplication::$debug))
			{
				EbApplication::$debug->peakMemory = memory_get_peak_usage();
				EbApplication::$debug->afterMemory	= memory_get_usage();
				EbApplication::$debug->generatedTime = microtime(true) - self::$time_start;
			}
									
			self::doFilter(self::$request,self::$response);
					
			self::$response->render();
		}
		
		/**
		* Execute the filter chain
		*/
		static public public function doFilter($request,$response)
		{
			foreach (EbApplication::$listFilters AS $filter)
			{
				$filter->doFilter($request,$response);
			}
		}		
		
		/**
		* This function executes an Event or an array of Events
		*/		
		static public function dispatchEvent($event)
		{
			if (!is_null($event))
			{
				if (is_array($event))
				{
					foreach ($event AS $eventItem)
					{
						self::dispatchEvent($eventItem);
					}
					
					return ;
				}
								
				if (isset(self::$commandEventMap[$event->type]) || strpos($event->type,"/") > 0)
				{
					if (isset(self::$commandEventMap[$event->type]))
					{
						$commandInstance = eb_module_file_new_instance(self::$commandEventMap[$event->type]);
					}
					else
					{
						if (strpos($event->type,"Command") != strlen($event->type) - 7)
						{
							$event->type .= "Command";
						}
						
						$commandInstance = eb_module_file_new_instance($event->type);
					}					
	
					if (!is_null($commandInstance))
					{
						$result = $commandInstance->execute($event);
						
						if (!is_null(EbApplication::$debug))
						{
							EbApplication::$debug->addCommand($event);
						}
					
						if ($result instanceof Event)
						{
							self::dispatchEvent($result);
						}
						else
						{
							return $result;
						}
					}
					else
					{
						throw new Exception("The event type '<i>$event->type</i>' is not defined and has no listener!");
					}
				}
				else
				{
					throw new Exception("The event type '<i>$event->type</i>' is not defined and has no listener!");
				}
			}
		}		
	}

?>